package com.atguigu.daijia.dispatch.xxl.job;

import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.atguigu.daijia.common.util.Convert;
import com.atguigu.daijia.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.daijia.dispatch.service.NewOrderService;
import com.atguigu.daijia.model.entity.dispatch.XxlJobLog;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/*
@description:
@ClassName JobHandler
@author chen
@create 2024-08-25 20:45
@Version 1.0
*/
@Component
public class JobHandler {

    @Resource
    private XxlJobLogMapper xxlJobLogMapper;

    @Resource
    private NewOrderService newOrderService;

    /*
     * @==============History===============<br/>
     * @Description //   乘客下单的任务调度,没一分钟一次去找满足条件的司机信息,然后把当前的订单信息放到这个司机的队列里面去
     * @Date 2024-8-25 20:58
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return void
     */
    @XxlJob("newOrderTaskHandler")
    public void newOrderTaskHandler()
    {
        XxlJobLog xxlJobLog = new XxlJobLog();
        // 记录开始时间
        long startTime = System.currentTimeMillis();
        try
        {
            //执行任务：搜索附近代驾司机
            newOrderService.executeTask(XxlJobHelper.getJobId());
            //成功状态
            xxlJobLog.setStatus(1);
        }
        catch (Exception e)
        {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            e.printStackTrace();
        }
        finally
        {
            // 记录任务调度日志
            // 记录结束时间
            long endTime = System.currentTimeMillis();
            long time = endTime - startTime;
            xxlJobLog.setTimes(Convert.toInt(time));
            xxlJobLogMapper.insert(xxlJobLog);
        }
    }

}
